package com.jwp.thread;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 
 * 线程池实现类
 * 
 * @author songdawei
 * @version 1.0
 * @date 2008-07-19
 */
public class ThreadPoolService {
	private static ThreadPoolService thisInstance = new ThreadPoolService();
	private static HashMap<String, ThreadPoolExecutor> poolsMap = new HashMap<String, ThreadPoolExecutor>();
	private List<ThreadPoolConfig> configPools = null; // 线程池配置项

	private ThreadPoolService() {
	}

	public static ThreadPoolService getInstance() {
		return thisInstance;
	}

	public void finalize() {
		this.destroyAll();
		try {
			super.finalize();
		} catch (Throwable e) {
			e.printStackTrace();
		}

	}

	/**
	 * 用指定的线程池执行一个任务
	 * 
	 * @param task -
	 *            任务对象
	 * @param poolName -
	 *            线程池名
	 */
	public void runTask(Runnable task, String poolName) {
		if (task != null && poolName != null) {
			ThreadPoolExecutor pool = Pool(poolName);
			pool.execute(task);
		}
	}

	/**
	 * 消毁线程池
	 * 
	 * @param poolName -
	 *            线程池名
	 * @return boolean
	 */
	public boolean destroyPool(String poolName) {
		boolean ret = false;
		synchronized (poolsMap) {
			ThreadPoolExecutor pool = poolsMap.get(poolName);
			if (pool != null) {
				pool.shutdown();
				poolsMap.remove(pool);
				ret = true;
			}

		}

		return ret;
	}

	public boolean destroyAll() {
		boolean ret = false;
		synchronized (poolsMap) {
			Iterator<String> it = poolsMap.keySet().iterator();
			while (it.hasNext()) {
				ThreadPoolExecutor pool = poolsMap.get(it.next());
				if (pool != null) {
					pool.shutdown();
				}
			}
			poolsMap.clear();

		}

		return ret;
	}

	public synchronized ThreadPoolExecutor Pool(String poolName) {

		return poolsMap.get(poolName);

	}

	public void init() {
		for (int i = 0; i < configPools.size(); i++) {
			ThreadPoolConfig poolConfig = configPools.get(i);
			BlockingQueue<Runnable> query = null;
			if ("1".equals(poolConfig.getQueueType())) {

				if (poolConfig.getMaxQueues().length() == 0) {
					query = new LinkedBlockingQueue<Runnable>();
				} else {
					query = new LinkedBlockingQueue<Runnable>(Integer
							.parseInt(poolConfig.getMaxQueues()));

				}

			} else if ("2".equals(poolConfig.getQueueType())) {

			}

			ThreadPoolExecutor threadPool = new ThreadPoolExecutor(Integer
					.parseInt(poolConfig.getMinThreads()), Integer
					.parseInt(poolConfig.getMaxThreads()), Long
					.parseLong(poolConfig.getKeepAlive()), TimeUnit.SECONDS,
					query, new ThreadPoolExecutor.CallerRunsPolicy());
			threadPool.allowCoreThreadTimeOut(true);
			poolsMap.put(poolConfig.getPoolName(), threadPool);
		}
	}

	public List<ThreadPoolConfig> getConfigPools() {
		return configPools;
	}

	public void setConfigPools(List<ThreadPoolConfig> configPools) {
		this.configPools = configPools;
	}

}
